package com.tedu.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.tedu.admin.dao.AdminUserDao;
import com.tedu.admin.dao.NewsDao;
import com.tedu.admin.dao.TopicDao;
import com.tedu.admin.pojo.AmindUser;
import com.tedu.pojo.Information;
import com.tedu.pojo.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminUserDao adminUserDao;
    @Autowired
    TopicDao topicDao;

    @Autowired
    NewsDao newsDao;


    /**
     * 显示后台登录页面
     * @return
     */
    @RequestMapping("/login")
    public String toLogin(){

        return "forward:/WEB-INF/admin/login.jsp";//转发到登录页面
    }


    /**
     * 用户登录功能
     * @return
     */
    @RequestMapping("/dologin")
    public String doLoin(AmindUser amindUser, HttpSession session){

        List<AmindUser> login = adminUserDao.login(amindUser);
        System.out.println("当前登录的用户："+login);
        //判断是否登录成功
        if (login.size()>0){
            //登录成功
            session.setAttribute("loginuser",login.get(0));
            return "forward:/WEB-INF/admin/index.jsp";
        }


        return "forward:/WEB-INF/admin/login.jsp";

    }


    /**
     * 显示党建信息类型管理页面
     * @return
     */
    @RequestMapping("/topic")
    public String toTopic(Model model){

        //获取全部的党建信息类型数据
        List<Topic> allTopic = topicDao.getAllTopic();
        //发送到页面
        model.addAttribute("allTopic",allTopic);

        return "forward:/WEB-INF/admin/newsTopic.jsp";//转发到登录页面
    }


    /**
     * 党建信息管理  分页
     * @return
     */
    @RequestMapping("/newsmanger")
    public String newsmanger(@RequestParam(name = "pageNo",defaultValue = "1",required = false) int pageNo,
                             Model model){
        List<Information> newsByNo = newsDao.getNewsByNo((pageNo - 1) * 50);
        model.addAttribute("newsByNo",newsByNo);

        return "forward:/WEB-INF/admin/news.jsp";
    }


    @RequestMapping("/ii")
    public String getdata(){
        return "forward:/WEB-INF/admin/iii.jsp";
    }



    @RequestMapping("/gettopic")
    @ResponseBody
    public String gettopic(){
        List<Topic> allTopic = topicDao.getAllTopic();
        //  接送
        String s = JSONObject.toJSONString(allTopic);
        return s;
    }

}
